package comboingRedient

import (
	"context"

	"gitee.com/wenchangshou/simple-admin-pancakebot-rpc/internal/svc"
	"gitee.com/wenchangshou/simple-admin-pancakebot-rpc/internal/utils/dberrorhandler"
	"gitee.com/wenchangshou/simple-admin-pancakebot-rpc/types/pancakebot"

	"github.com/suyuan32/simple-admin-common/msg/errormsg"

	"github.com/suyuan32/simple-admin-common/utils/pointy"
	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateComboIngredientLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateComboIngredientLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateComboIngredientLogic {
	return &UpdateComboIngredientLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateComboIngredientLogic) UpdateComboIngredient(in *pancakebot.ComboIngredientInfo) (*pancakebot.BaseResp, error) {
	query := l.svcCtx.DB.ComboIngredient.UpdateOneID(*in.Id).
		SetNotNilComboID(in.ComboId).
		SetNotNilIngredientID(in.IngredientId).
		SetNotNilIsRequired(in.IsRequired).
		SetNotNilPrice(in.Price)

	if in.Quantity != nil {
		query.SetNotNilQuantity(pointy.GetPointer(int(*in.Quantity)))
	}

	err := query.Exec(l.ctx)

	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, in)
	}

	return &pancakebot.BaseResp{Msg: errormsg.UpdateSuccess}, nil
}
